class JobController < ApplicationController
  layout "main"

  def alljob
    jobs = Job.find(:all, :conditions=>["user_id=? AND j_status=?", session[:user_id], "Active"], :order=>"id desc")
    jobs.each do |job|
      if job.expired_date
        if Date.today>job.expired_date
          job.j_status = 'Expired'
          job.save
        end
      end
    end
    @jobs = Job.find(:all, :conditions=>["user_id=? AND j_status=?", session[:user_id], "Active"], :order=>"id desc")
    render :action=>"alljob"
  end

  def addjob
    job = Job.new(params[:job])
    job.user_id = session[:user_id]
    expired_date = Date.today + params[:life].to_i
    job.expired_date = expired_date
    job.save
    redirect_to :action=>"alljob"
  end

  def deletejob
    Job.find(params[:id]).destroy
    redirect_to :action=>"alljob"
  end

  def jobdetail
    @job = Job.find(params[:id])
    render :action=>"jobdetail"
  end

  def show_people
    @users = get_people(params[:id])
    render :action=>"show_people"
  end

  def get_people(job_id)
    job = Job.find(job_id)
    gender = job.gender
    min_age = job.min_age
    max_age = job.max_age
    province = job.province
    education_id = job.education_id
    syllabus_id = job.syllabus_id
    qwhere = Array.new
    qwhere = qwhere << "role!='C'"
    if gender!='all'
      qwhere = qwhere << "gender='#{gender}'"
    end
    if min_age
      date = Date.today
      min_date = Date.parse("#{date.year-min_age.to_i}-#{date.month}-#{date.day}")
      qwhere = qwhere << "dob<='#{min_date}'"
    end
    if max_age
      date = Date.today
      max_date = Date.parse("#{date.year-max_age.to_i}-#{date.month}-#{date.day}")
      qwhere = qwhere << "dob>='#{max_date}'"
    end
    if province!='all'
      qwhere = qwhere << "province='#{province}'"
    end
    if education_id!=0
      qwhere = qwhere << "education_id>=#{education_id}"
    end
    user_list = Array.new
    word = ""
    if syllabus_id!=0
      word =  " AND s.id=#{syllabus_id}"
      f_sql = "SELECT DISTINCT(r.user_id) AS user_id FROM users u,syllabuses s,syllabus_openings so,registrations r,registration_courses rc,certificate_course_rules ccr,courses c WHERE c.id=ccr.course_id AND rc.course_id=c.id AND ccr.syllabus_id=s.id AND rc.registration_id=r.id AND s.id=so.syllabus_id AND r.syllabus_opening_id=so.id AND r.user_id=u.id AND rc.grade<=ccr.minimum_grade#{word}"
      filtered_users = Registration.find_by_sql f_sql
      filtered_users.each do |fu|
        user_list = user_list << fu.user_id
      end
      if user_list.size>0
        qwhere = qwhere << "id IN (#{user_list.uniq.join(',')})"
      else
        qwhere = qwhere << "id IN (0)"
      end
    else

    end

    sql = ""
    if qwhere.size>0
      sql = "select * from users where #{qwhere.join(' AND ')} order by fname"
    else
      sql = "select * from users order by fname"
    end
    users = User.find_by_sql sql
    return users
  end

  def match
    jobs = Job.find(:all, :conditions=>["j_status=?", "Active"], :order=>"id desc")
    jobs.each do |job|
      if job.expired_date
        if Date.today>job.expired_date
          job.j_status = 'Expired'
          job.save
        end
      end
    end
    @jobs = Job.find(:all, :conditions=>["j_status=?", "Active"])
    render :action=>"match"
  end

end
